﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Meraz.Core.Model;
using Ninject;
using PetaPoco;

namespace Meraz.Core.Repositories
{
    public interface IFacebookTopicPostLinkRepository
    {
        FacebookTopicPostLink GetById(long boardID, int topicID, int postID, int sequence);
        List<FacebookTopicPostLink> GetTopicPostLinks(long boardID, int topicID, int postID);
        List<FacebookTopicPostLink> GetTopicLinks(long boardID, int topicID);
        void Add(FacebookTopicPostLink entity);
        void Delete(FacebookTopicPostLink entity);
        void Delete(long boardID, int topicID, int postID);
        List<FacebookTopicPostLink> All();
        void Save(FacebookTopicPostLink entity);
        List<String> GetSources();
    }
    public class FacebookTopicPostLinkRepository : IFacebookTopicPostLinkRepository
    {
        [Inject]
        public Database DB { get; set; }

        public FacebookTopicPostLink GetById(long boardID, int topicID, int postID, int id)
        {
            return DB.FirstOrDefault<FacebookTopicPostLink>("WHERE BoardID=@0 AND TopicID=@1 AND PostID=@2 AND ID=@3", boardID, topicID, postID, id);
        }

        public List<FacebookTopicPostLink> GetTopicPostLinks(long boardID, int topicID, int postID)
        {
            return DB.Query<FacebookTopicPostLink>("WHERE BoardID=@0 AND TopicID=@1 AND PostID=@2 order by ID", boardID, topicID, postID).ToList();
        }

        public List<FacebookTopicPostLink> GetTopicLinks(long boardID, int topicID)
        {
            return DB.Query<FacebookTopicPostLink>("WHERE BoardID=@0 AND TopicID=@1 order by ID", boardID, topicID).ToList();
        }

        public void Add(FacebookTopicPostLink entity)
        {
            throw new NotImplementedException();
        }

        public void Delete(FacebookTopicPostLink entity)
        {
            DB.Delete<FacebookTopicPostLink>(entity);
        }

        public void Delete(long boardID, int topicID, int postID)
        {
            DB.Delete<FacebookTopicPostLink>("WHERE BoardID=@0 AND TopicID=@1 AND PostID=@2", boardID, topicID, postID);
        }

        public List<FacebookTopicPostLink> All()
        {
            throw new NotImplementedException();
        }

        public void Save(FacebookTopicPostLink entity)
        {
            var p = DB.SingleOrDefault<FacebookTopicPostLink>("SELECT * FROM facebooklink WHERE BoardID=@0 AND TopicID = @1 AND PostID = @2 AND ID = @3", entity.BoardID, entity.TopicID, entity.PostID, entity.ID);
            if (p == null)
            {
                DB.Insert(entity);
            }
            else
            {
                DB.Update(entity);
            }
        }

        public List<String> GetSources()
        {
            return DB.Query<String>("Select distinct source from facebooklink where source <> '' order by source").ToList();
        }

    }
}
